﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.SubGroup;
using WcfService1.DTO;

namespace MSIACCSL2.Views.PurchaseOrder
{
    public partial class FrmAdd : Page
    {
        public PurchaseOrderViewModel VM
        {
            get { return (PurchaseOrderViewModel)this.DataContext; }
        }

        public PurchaseOrderWrapViewModel WMW
        {
            get { return (this.Resources["POW"] as PurchaseOrderWrapViewModel); }
        }

        private TextBox TxtSubgroup;
        private PurchaseOrderDetailDTO podetail;
        public FrmAdd()
        {
            InitializeComponent();
            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as PurchaseOrderWrapViewModel).PurchaseOrderViewModel =
                                        (PurchaseOrderViewModel)this.DataContext;
                               };


            this.Loaded += (o, e) =>
            {
                txtSupplier.Focus();
            };

            this.txtSupplier.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //VM.GetSupplier(txtSupplier.Text);
                    SupplierSearch supplierSearch = new SupplierSearch();
                    supplierSearch.Closed += new EventHandler(supplierSearch_Closed);
                    supplierSearch.Show();
                    supplierSearch.txtSupplierName.Text = txtSupplier.Text;
                    supplierSearch.txtSupplierName.Focus();
                }
            };

            this.KeyDown += (x, y) =>
            {
                if (y.Key == Key.T && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.AddPurchaseOrderDetail(null);
                else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.DeletePurchaseOrderDetail(null);
                else if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.SavePurchaseOrder(VM.PurchaseOrder);
                else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.Cancel(null);
            };
            
        }

        void supplierSearch_Closed(object sender, EventArgs e)
        {
            VM.PurchaseOrder.Supplier = (sender as SupplierSearch).Supplier;
            txtSupplier.Focus();
        }
      

        private void KeyDo_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var SearchSubgroup = new SearchSubgroup();
                TxtSubgroup = sender as TextBox;
                podetail = TxtSubgroup.DataContext as PurchaseOrderDetailDTO;
                SearchSubgroup.Closed += new EventHandler(SearchSubgroup_Closed);
                SearchSubgroup.txtSearch.GotFocus += (x, y) =>
                                                         {
                                                             (x as TextBox).Text = (sender as TextBox).Text; 
                                                             (x as TextBox).SelectAll();
                                                         };
                SearchSubgroup.Show();
            }
        }

        void SearchSubgroup_Closed(object sender, EventArgs e)
        {
            var Subgroup = (sender as SearchSubgroup).dg1.SelectedItem as SubGroupDTO;
            podetail.SubGroup = Subgroup;
            dg1.Focus();
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        private void KeyDo_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

      

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	VM.CloseStatus();
        }
       

        private void TextBox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty((sender as TextBox).Text)) return;

            foreach (var purchaseOrderDetailDto in VM.PurchaseOrder.PurchaseOrderDetails)
                purchaseOrderDetailDto.Price = purchaseOrderDetailDto.Price * Decimal.Parse((sender as TextBox).Text);
            WMW.GetTotalDetail(null);
        }
      

    }
}

